package com.tyk.bookstore.back.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.tyk.bookstore.back.common.core.model.result.R;
import com.tyk.bookstore.back.common.core.mvc.annotation.UnderlineToCamelModel;
import com.tyk.bookstore.back.system.model.query.RoleAddQuery;
import com.tyk.bookstore.back.system.model.query.RolePageQuery;
import com.tyk.bookstore.back.system.model.query.RoleUpdateQuery;
import com.tyk.bookstore.back.system.model.vo.RolePageVo;
import com.tyk.bookstore.back.system.model.vo.RoleSuggestionVo;
import com.tyk.bookstore.back.system.model.vo.RoleVo;
import com.tyk.bookstore.back.system.service.RoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "系统管理/角色管理", description = "角色管理")
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("api/system/role")
public class RoleController {

    private final RoleService roleService;

    @Operation(summary = "分页查询角色列表", description = "分页查询角色列表")
    @SaCheckPermission("system:role:get")
    @GetMapping("page")
    public R<RolePageVo> getPage(
            @Validated @UnderlineToCamelModel RolePageQuery query
    ) {
        return R.ok(roleService.getPage(query));
    }

    @Operation(summary = "根据ID获取角色信息", description = "根据ID获取角色信息")
    @SaCheckPermission("system:role:update")
    @GetMapping("{id}")
    public R<RoleVo> getById(
            @Validated @NotNull @Min(0)
            @PathVariable("id") Long id
    ) {
        return R.ok(roleService.getById(id));
    }


    @Operation(summary = "新增角色", description = "新增角色")
    @SaCheckPermission("system:user:add")
    @PostMapping("")
    public R<Object> addRole(
            @RequestBody @Validated RoleAddQuery query
    ) {
        roleService.addRole(query);
        return R.ok();
    }

    @Operation(summary = "根据ID修改角色信息", description = "根据ID修改角色信息")
    @SaCheckPermission("system:role:update")
    @PutMapping
    public R<Object> updateById(
            @RequestBody @Validated RoleUpdateQuery query
    ) {
        roleService.updateById(query);
        return R.ok();
    }

    @Operation(summary = "根据ID删除角色信息", description = "根据ID删除角色信息")
    @SaCheckPermission("system:role:delete")
    @DeleteMapping("{id}")
    public R<Object> deleteById(
            @Validated @NotNull @Min(0)
            @PathVariable("id") Long id
    ) {
        roleService.deleteById(id);
        return R.ok();
    }

    @Operation(summary = "查询角色建议列表", description = "查询角色建议列表")
    @SaCheckPermission({"system:user:suggestion:list"})
    @GetMapping("suggestion/list")
    public R<List<RoleSuggestionVo>> getSuggestionList() {
        return R.ok(roleService.getSuggestionList());
    }
}
